package com.company.Tree;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}13:35
 */
public class Tree {
    private int no;
    private String name;
    private Tree left;
    private Tree right;
    private int leftType;
    private int rightType;

    public int getLeftType() {
        return leftType;
    }

    public void setLeftType(int leftType) {
        this.leftType = leftType;
    }


    public int getRightType() {
        return rightType;
    }

    public void setRightType(int rightType) {
        this.rightType = rightType;
    }

    public Tree(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Tree getLeft() {
        return left;
    }

    public void setLeft(Tree left) {
        this.left = left;
    }

    public Tree getRight() {
        return right;
    }

    public void setRight(Tree right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "Tree{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    //前序遍历
    public void preOrder(){
        System.out.println(this);
        //递归向左子树前序遍历
        if (this.left != null){
            this.left.preOrder();
        }
        //递归向右子树前序遍历
        if (this.right != null){
            this.right.preOrder();
        }
    }
    //中序遍历
    public void infixOrder(){
        //递归向左子树中序遍历
        if (this.left!=null){
            this.left.infixOrder();
        }
        //输出父节点
        System.out.println(this);
        if (this.right!=null){
            this.right.infixOrder();
        }
    }
    //后序遍历
    public void postOrder(){
        if (this.left!=null){
            this.left.postOrder();
        }
        if (this.right!=null){
            this.right.postOrder();
        }
        System.out.println(this);
    }

    //前序遍历查找
    Tree preOrderSearch(int no){
        Tree tree = null;
        if (this.no==no){
            return this;
        }

        if (this.left!=null){
            tree = this.left.preOrderSearch(no);
        }
        if (tree!=null){
            return tree;
        }

        if (this.right!=null){
            tree = this.right.preOrderSearch(no);
        }
        return tree;
    }

    //中序遍历查找
    Tree indixOrderSearch(int no){
        Tree tree = null;
        if (this.left!=null){
            tree = this.left.indixOrderSearch(no);
        }
        if (tree!=null){
            return tree;
        }

        if (this.no==no){//左右子树都没找到，父节点（遍历最后一个节点）比较
            return this;
        }

        if (this.right!=null){
            tree = this.right.indixOrderSearch(no);
        }
        if (tree!=null){
            return tree;
        }
        return tree;
    }

    //后序遍历查找
    Tree postOrderSearch(int no){
        Tree tree = null;
        if (this.left!=null){
            tree = this.left.postOrderSearch(no);
        }
        if (tree!=null){
            return tree;
        }

        if (this.right!=null){
            tree = this.right.postOrderSearch(no);
        }
        if (tree!=null){
            return tree;
        }
        if (this.no==no){
            return this;
        }
        return tree;
    }
    void delNode(int no){
        if (this.left!=null && this.left.no == no){
            this.left = null;
            return;
        }
        if (this.right!=null && this.right.no == no){
            this.right = null;
            return;
        }
        if (this.left!=null){
            this.left.delNode(no);
        }
        if (this.right!=null){
            this.right.delNode(no);
        }
    }
}
